//
//  GameViewController.swift
//  DouYuZB
//
//  Created by 梁森 on 2020/4/21.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

private let kMargin:CGFloat = 10
private let kItemWidth:CGFloat = (kScreenWidth - 2 * kMargin) / 3
private let kItemHeight:CGFloat = kItemWidth
private let kHeaderId:String = "kHeaderId"
private let kHeaderH:CGFloat = 50.0
/// 常见视图高度
private let kCommonViewH:CGFloat = 50.0
/// 常见游戏高度
private let kCommonGameH:CGFloat = 90.0
private let kTopHeight:CGFloat = kCommonViewH + kCommonGameH

class GameViewController: BaseViewController {

    // MARK:懒加载
    fileprivate lazy var collectionView:UICollectionView = { [unowned self] in
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: kItemWidth, height: kItemHeight)
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: kMargin, bottom: 0, right: kMargin)
        flowLayout.headerReferenceSize = CGSize(width: kScreenWidth, height: kHeaderH)
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        let collectionFrame = CGRect(x: 0, y: 0, width: kScreenWidth, height: self.view.bounds.size.height)
        let collectionView = UICollectionView(frame: collectionFrame, collectionViewLayout: flowLayout)
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // 让宽高随着父试图变化而变化
        collectionView.contentInset = UIEdgeInsets(top: kTopHeight, left: 0, bottom: 0, right: 0)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(RecommendGameCell.self, forCellWithReuseIdentifier: RecommendGameCell.reuseIdentifier())
        collectionView.register(UINib(nibName: "RecommendColHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kHeaderId)
        collectionView.backgroundColor = UIColor.white
        return collectionView
    }()
    /// 常见视图
    fileprivate lazy var commonView:RecommendColHeader = {
        let commonView = RecommendColHeader.recommendColHeader()
        commonView.frame = CGRect(x: 0, y: -kTopHeight, width: kScreenWidth, height: kCommonViewH)
        commonView.moreBtn.isHidden = true
        commonView.titleLbl.text = "常见"
        commonView.iconView.image = UIImage(named: "Img_orange")
        return commonView
    }()
    /// 常见游戏视图
    fileprivate lazy var commonGame:RecommendGameView = {
        let commonGame = RecommendGameView(frame: CGRect(x: 0, y: -kCommonGameH, width: kScreenWidth, height: kCommonGameH))
        return commonGame
    }()
    
    fileprivate lazy var gameViewModel = GameViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
       print("GameViewController😄😄😄😄");
        setupUI()
        getGameData()
    }

}
// MARK:遵守协议
extension GameViewController:UICollectionViewDelegate, UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return gameViewModel.allGameData.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RecommendGameCell.reuseIdentifier(), for: indexPath) as! RecommendGameCell
        cell.backgroundColor = UIColor.white
        let gameModel = gameViewModel.allGameData[indexPath.item]
        cell.anchorGroup = gameModel
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kHeaderId, for: indexPath) as! RecommendColHeader
        header.titleLbl.text = "全部"
        header.iconView.image = UIImage(named: "Img_orange")
        header.moreBtn.isHidden = true
        return header
    }
    
}
// MARK:设置UI
extension GameViewController{
    override func setupUI(){
        view.addSubview(collectionView)
        super.setupUI()
        collectionView.addSubview(commonView)
        collectionView.addSubview(commonGame)
    }
}
// MARK:网络数据
extension GameViewController{
    fileprivate func getGameData(){
        gameViewModel.requestAllGameData(finishCallBack: { [unowned self] in
            self.collectionView.reloadData()
            let commonGames = Array(self.gameViewModel.allGameData[1..<10])
            self.commonGame.groups = commonGames
            
            self.requestDataFinish()
        }) {
            
        }
    }
}
